package com.project.livetv.fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.project.livetv.adapter.MyFragmentPageAdapter;
import com.project.livetv.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by li on 2016/5/9.
 */
public class MainFragment extends BaseFragment {
    private String message;
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private List<Fragment> fragments;
    private MyFragmentPageAdapter fragmentAdapter;
    private View view;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        message = bundle.getString("message");
        fragments = new ArrayList<>();

    }

    public static MainFragment newInstance(String message) {

        Bundle args = new Bundle();
        args.putString("message", message);
        MainFragment fragment = new MainFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.main_fragment_layout, container, false);
        initView(rootView);
        initData();
        setData();
        return rootView;
    }

    private void initData() {
        fragmentAdapter = new MyFragmentPageAdapter(getChildFragmentManager(), fragments,view);
    }

    private void initView(View rootView) {
        mTabLayout = (TabLayout) rootView.findViewById(R.id.tl_mainfragment_position);
        mViewPager= (ViewPager) rootView.findViewById(R.id.viewpaget_mainfragment_position);
        view=LayoutInflater.from(getContext()).inflate(R.layout.tab_view,null);
        String attention = "关注";
      //  String hot = "热门";
        String latest = "最新";
        mTabLayout.addTab(mTabLayout.newTab().setText(attention));
        mTabLayout.addTab(mTabLayout.newTab().setCustomView(view));
        mTabLayout.addTab(mTabLayout.newTab().setText(latest));
        //getChildFragmentManager().beginTransaction().add(R.id.container_mainfragment_position,new AttentionFragment()).commit();
        AttentionFragment attentionFragment = new AttentionFragment();
        HotFragment hotFragment = new HotFragment();
        LatestFragment latestFragment = new LatestFragment();
        fragments.add(attentionFragment);
        fragments.add(hotFragment);
        fragments.add(latestFragment);
    }

    private void setData() {
       mViewPager .setAdapter(fragmentAdapter);
        mTabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                TextView hotText;
                ImageView hotImage;
                hotText = (TextView) view.findViewById(R.id.textview_tablayout_position);
                hotImage = (ImageView) view.findViewById(R.id.icon_tablayout_position);
                if (position == 1) {
                    hotImage.setVisibility(ImageView.VISIBLE);
                    hotText.setTextColor(Color.DKGRAY);
                } else {
                    hotImage.setVisibility(ImageView.INVISIBLE);
                    hotText.setTextColor(Color.WHITE);
                }
                fragmentAdapter.notifyDataSetChanged();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
}
